﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
using CI.PW.New_Scenario_Wizard;
using CI.Mesopotamia;

namespace CI.PW
{
    /// <summary>
    /// Interaction logic for SimulationOption.xaml
    /// </summary>
    public partial class SimulationOption
    {
        NavigationService _navService;
        readonly SimEnv _simEnv = SimEnv.Instance;
        private Progress _progress;

        /// <summary>
        /// Simulation option
        /// </summary>
        public SimulationOption()
        {
            InitializeComponent();

            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal,
                (Action)delegate
                {
                    chkSceneAndOrganisms.IsEnabled = ScenarioGrid.GridOnline();
                });
        }

        /// <summary>
        /// get the scenes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkSceneChecked(object sender, RoutedEventArgs e)
        {
            LoadScenario();
        }

        /// <summary>
        /// action when they select the wizard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkWizardChecked(object sender, RoutedEventArgs e)
        {
            // go to page
            if (_navService == null)
                _navService = NavigationService.GetNavigationService(this);

            // restart if not running
            if ((_simEnv.HostEnv != null) && (!_simEnv.HostEnv.StartTime.Equals(new DateTime())))
            {
                _simEnv.TriggerScenarioRestart("Start Manually Selected Simulation");
            }

            var finish = new Finish();
            _navService.Navigate(finish);
        }

        /// <summary>
        /// set the selected item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LboxScenariosSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // make sure we have a value
            if (sender == null)
                return;

            var scenarioName = ((ListBox)sender).SelectedItem;

            if (scenarioName == null)
                return;

            // set the scenario name
            Display.ScenarioName = scenarioName.ToString().Trim();

            if (chkSceneAndOrganisms.IsChecked.GetValueOrDefault())
            {
                var orgSelection = new OrganismSelection(Display.ScenarioName);

                // go to page
                if (_navService == null)
                    _navService = NavigationService.GetNavigationService(this);

                _navService.Navigate(orgSelection);
                return;
            }

            // start as normal
            ChkWizardChecked(null, null);
        }

        private void LoadScenario()
        {
            lboxScenarios.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                (Action)delegate
                {
                    _progress = new Progress();
                    _progress.Show();

                    // get the list of scenarios
                    var sf = new Mesopotamia.ScenarioFactory();
                    var scenario = sf.GetScenario();
                    var scenarios = scenario.GetScenariosAvailable();
                    // add them to the list box
                    lboxScenarios.ItemsSource = scenarios;

                    // unhide the group and populate the list box
                    SceneOptions.IsEnabled = true;

                    if (_progress != null)
                        _progress.Close();

                    _progress = null;
                });
        }

        private void ChkSceneAndOrganismsChecked(object sender, RoutedEventArgs e)
        {
            LoadScenario();
        }
    }
}
